<?php
namespace api\modules\mp\models;

use yii\base\Model;
use common\tool\Curd;
use yii\web\NotFoundHttpException;

class Enroll extends Model {
    use Curd;

    public static function tableName() {
        return 'mp_boat_enroll';
    }

    public function rules() {
        return [
            [
                [
                    'cabinId',
                ],
                'required'
            ],
            [
                [
                    'boatId',
                    'batchId',
                ],
                'number'
            ],
            [
                [
                    'chinese_name',
                    'openid',
                    'english_name',
                    'birthday',
                    'phone',
                    'wechat',
                    'id_number',
                    'passport'
                ],
                'string'
            ],
            ['email','email'],
            [
                [
                    'ctime',
                    'utime'
                ],
                'datetime',
                'format' => 'yyyy-MM-dd HH:mm:ss'
            ],
            [
                [
                    'gender',
                ],
                'in',
                'range' => [
                    0,
                    1,
                    2
                ]
            ],
        ];
    }

    public static function build($data = []): ?self {
        if (!empty($data['id'])) {
            $one = self::getOne("id={$data['id']}");
            if (!$one) throw new NotFoundHttpException();
            $data = array_merge($one, $data);
        }
        return new self($data);
    }

    public $id;
    public $batchId;
    public $boatId;
    public $cabinId;
    public $openid;
    public $ctime;
    public $utime;
    public $status = self::STATUS_UNDETERMINED;
    public $chinese_name='';
    public $english_name = '';
    public $birthday;
    public $gender=0;
    public $phone='';
    public $email='';
    public $wechat='';
    public $passport='';
    public $id_number='';
    //状态1待确认2已确认3取消4拒绝
    const STATUS_UNDETERMINED = 1;
    const STATUS_CONFIRM = 2;
    const STATUS_CANCEL = 3;
    const STATUS_REJECT = 4;

    public function attributeLabels() {
        return [
            'batchId' => '批次id',
            'ctime' => 'c时间',
            'utime' => 'u时间',
            'cabinId' => '房间ID',
        ];
    }

    public function save() {
        $data = [
            'batch_id'=>$this->batchId,
            'boat_id' => $this->boatId,
            'cabin_id' => $this->cabinId,
            'openid' => $this->openid,
            'status' => $this->status,
            'chinese_name' => $this->chinese_name,
            'english_name' => $this->english_name,
            'birthday' => $this->birthday,
            'gender' => $this->gender,
            'phone' => $this->phone,
            'email' => $this->email,
            'wechat' => $this->wechat,
            'passport' => $this->passport,
            'id_number' => $this->id_number,
        ];
        $now = date('Y-m-d H:i:s');
        if ($this->id) {
            $data['utime'] = $now;
            self::update($data, 'id=:id', [':id' => $this->id]);
        } else {
            $data['ctime'] = $data['utime'] = $now;
            $id = self::insert($data);
            $this->id = $id;
        }
    }

    public function confirm() {
        return self::update(['status' => self::STATUS_CONFIRM], 'id=:id', [':id' => $this->id]);
    }

    public function reject() {
        return self::update(['status' => self::STATUS_REJECT], 'id=:id', [':id' => $this->id]);
    }

    public function cancel() {
        return self::update(['status' => self::STATUS_CANCEL], 'id=:id', [':id' => $this->id]);
    }

    public function info() {
        return $this->getAttributes();
    }
}
